﻿@inject IStringLocalizer<ValidateForms> Localizer

<ValidateForm Model="@Model" OnFieldValueChanged="@OnFiledChanged"
              OnValidSubmit="@OnValidSubmit1" OnInvalidSubmit="@OnInvalidSubmit1">
    <div class="row g-3">
        <div class="col-12">
            <BootstrapInput @bind-Value="@Model.Name" DisplayText="@Localizer["LongDisplayText"]" />
        </div>
    </div>
    <div class="row g-3 form-inline mt-0">
        <div class="col-12">
            <BootstrapInput @bind-Value="@Model.Address" DisplayText="@Localizer["LongDisplayText"]" ShowLabelTooltip="true" />
        </div>
        <div class="col-12">
            <Button ButtonType="ButtonType.Submit" Icon="fa-fw fa-solid fa-floppy-disk" Text="@Localizer["ValidateFormsSubmitButtonText"]" IsAsync="true"></Button>
        </div>
    </div>
</ValidateForm>
<ConsoleLogger @ref="Logger" class="mt-3" />

@code {
    [NotNull]
    private Foo? Model { get; set; }

    [NotNull]
    private ConsoleLogger? Logger { get; set; }

    private async Task OnInvalidSubmit1(EditContext context)
    {
        await Task.Delay(1000);
        Logger.Log(Localizer["OnInvalidSubmitLog"]);
    }

    private async Task OnValidSubmit1(EditContext context)
    {
        await Task.Delay(1000);
        Logger.Log(Localizer["OnValidSubmitLog"]);
    }

    private void OnFiledChanged(string field, object? value)
    {
        Logger.Log($"{field}:{value}");
    }

    /// <summary>
    /// OnInitializedAsync 方法
    /// </summary>
    /// <returns></returns>
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        // 切换线程 模拟异步通过 webapi 加载数据
        await Task.Yield();

        Model = new() { Name = "Name", Education = EnumEducation.Primary, DateTime = DateTime.Now };
    }
}
